Page({
  onShareAppMessage() {
    return {
      title: 'live-player',
      path: 'packageComponent/pages/media/live-player/live-player'
    }
  },
  data: {
    hidden:false,
    theme: 'light',
    videoSrc:'rtmp://dxplay.scwy.cn:1935/live/weiyuanzonghepingdao',
    mode: 'live',
    autoplay:true,
    muted:false,
    orientation:'vertical',
    objectFit : 'contain',
    minCache : "1",
    maxCache : "1",
    soundMode : "speaker",
    autoPauseIfNavigate : true,
    autoPauseIfOpenNative : true,
    pictureInPictureMode : [],
    enableAutoRotation : false,
    direction : 0 ,
    snapshotSrc : null,
    showtc: false

  },
  onReady() {
    this.ctx = mp.createLivePlayerContext('player')
  },
  handleScanQRCode() {
    mp.scanCode({
      complete: (res) => {
        const {result} = res
        this.setData({
          videoSrc: result
        })
      },
    })
  },
  handleLivePlayerStateChange(e) {
    console.log('live-player code:', e.detail.code)
  },
  handleLivePlayerError(e) {
    console.error('live-player error:', e.detail.errMsg)
  },
  handlePlay() {
    this.ctx.play({
      success: () => {
        console.log('play success')
      },
      fail: () => {
        console.log('play fail')
      },
      complete: (res) => {
        console.log('play complete',res)
      }
    })
  },
  handlePause() {
    this.ctx.pause({
      success: () => {
        console.log('pause success')
      },
      fail: () => {
        console.log('pause fail')
      },
      complete: (res) => {
        console.log('pause complete',res)
      }
    })
  },
  handleStop() {
    this.ctx.stop({
      success: () => {
        console.log('stop success')
      },
      fail: () => {
        console.log('stop fail')
      },
      complete: (res) => {
        console.log('stop complete',res)
      }
    })
  },
  handleResume() {
    this.ctx.resume({
      success: () => {
        console.log('resume success')
      },
      fail: () => {
        console.log('resume fail')
      },
      complete: (res) => {
        console.log('resume complete',res)
      }
    })
  },
  handleMute() {
    this.ctx.mute({
      success: () => {
        console.log('mute success')
      },
      fail: () => {
        console.log('mute fail')
      },
      complete: (res) => {
        console.log('mute complete',res)
      }
    })
  },
  handleVideoSrcInput(e) {
    this.setData({
      videoSrc: e.detail.value
    })
  },
  onUnload() {
    if (mp.offThemeChange) {
      mp.offThemeChange()
    }
  },
  onLoad() {
    this.setData({
      theme: mp.getSystemInfoSync().theme || 'light'
    })

    if (mp.onThemeChange) {
      mp.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  },
      //常规参数设置 
      setParams(e) {
        const { key } =  e.currentTarget.dataset
        var tempParams = null
        if(parseInt(e.detail.value)){
          tempParams =  parseInt(e.detail.value)
        }
        else if(e.detail.value == "null"){
          tempParams = null
        }
        else if (e.detail.value == "true" || e.detail.value == "false"){
          tempParams = JSON.parse(e.detail.value)
        }
        else if (e.detail.value.search(/{/)==0 && e.detail.value.search(/}/)==e.detail.value.length-1) {
          tempParams = eval("(" + e.detail.value + ")")
        }
        else{
          tempParams = e.detail.value
        }
        this.setData({
            [key]: tempParams
        })
        console.log(key," value:",this.data.param,",",key," type:",typeof tempParams)
      },
      eventLog(e) {
        console.log('事件触发', e.type, e.detail)
      },
      exitFullScreen() {
        this.ctx.exitFullScreen({
          success: () => {
            console.log('exitFullScreen success')
          },
          fail: () => {
            console.log('exitFullScreen fail')
          },
          complete: (res) => {
            console.log('exitFullScreen complete',res)
          }
        })
      },
      exitPictureInPicture() {
        this.ctx.exitPictureInPicture({
          success: () => {
            console.log('exitPictureInPicture success')
          },
          fail: () => {
            console.log('exitPictureInPicture fail')
          },
          complete: (res) => {
            console.log('exitPictureInPicture complete',res)
          }
        })
      },
      requestFullScreen() {
        this.ctx.requestFullScreen({
          direction: this.data.direction,
          success: () => {
            console.log('requestFullScreen success')
          },
          fail: () => {
            console.log('requestFullScreen fail')
          },
          complete: (res) => {
            console.log('requestFullScreen complete',res)
          }
        })
      },
      requestPictureInPicture() {
        this.ctx.requestPictureInPicture({
          success: () => {
            console.log('requestPictureInPicture success')
          },
          fail: () => {
            console.log('requestPictureInPicture fail')
          },
          complete: (res) => {
            console.log('requestPictureInPicture complete',res)
          }
        })
      },
      snapshot() {
        this.ctx.snapshot({
          quality:'raw',  //raw：原图、compressed：压缩图
          sourceType : "stream",  //stream：截取视频源、view：截取渲染后的画面
          success: (res) => {
            console.log('snapshot success',res)
            this.setData({
              snapshotSrc: res.tempImagePath
            })
          },
          fail: (res) => {
            console.log('snapshot fail',res)
          },
          complete: (res) => {
            console.log('snapshot complete',res)
          }
        })
      }, 

      tcxrClick() {
        this.setData({
          showtc: true
        })
      }
})
